package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Citys;
import ex.com.jm.food.entity.Countys;
import ex.com.jm.food.entity.Province;
import ex.com.jm.food.entity.Streets;
import ex.com.jm.food.service.AddressService;

/**
 *地址select框的二级联动
 * @author zhaojw
 *
 */
public class AddressAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private int proId;
	private int cityId;
	private int countyId;
	private AddressService addressService;
	public int getProId() {
		return proId;
	}
	public void setProId(int proId) {
		this.proId = proId;
	}
	public int getCityId() {
		return cityId;
	}
	public void setCityId(int cityId) {
		this.cityId = cityId;
	}
	public int getCountyId() {
		return countyId;
	}
	public void setCountyId(int countyId) {
		this.countyId = countyId;
	}
	
	public AddressService getAddressService() {
		return addressService;
	}
	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}
	public void getProvinces(){
		List<Province> provinces=this.getAddressService().getProvinces();
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		for (Province province : provinces) {
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("proId", province.getProId());
			map.put("proName", province.getProName());
			list.add(map);
		}
		JSONArray jsonPro=JSONArray.fromObject(list);
		
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8"); 
		 PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print(jsonPro);
	}
	
	public void getCitys(){
		List<Citys> citys=this.getAddressService().getCitys(proId);
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		for (Citys city : citys) {
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("cityId", city.getCityId());
			map.put("cityName", city.getCityName());
			list.add(map);
		}
		 JSONArray jsonCity=JSONArray.fromObject(list);
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8"); 
		 PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print(jsonCity);
			out.flush();
			out.close();
	}
	
	public void getCountys(){
		List<Countys> countys=this.getAddressService().getCountys(cityId);
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		for (Countys county : countys) {
			Map<String , Object> map=new HashMap<String, Object>();
			map.put("countyId", county.getCountyId());
			map.put("countyName", county.getCountyName());
			list.add(map);
		}
		JSONArray jsonCoun=JSONArray.fromObject(list);
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8"); 
		 PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print(jsonCoun);
	}
	
	public void getStreets(){
		List<Streets> streets=this.getAddressService().getStreets(countyId);
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		for (Streets street : streets) {
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("streetId", street.getStreetId());
			map.put("streetName", street.getStreetName());
			list.add(map);
		}
		JSONArray jsonStreet=JSONArray.fromObject(list);
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8"); 
		 PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print(jsonStreet);
	}
}
